package com.apache.passport.service.plugins;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.datasource.ConnctionData;
import com.apache.database.datasource.DynamicDataSource;
import com.apache.database.datasource.DynamicDataSourceHolder;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.passport.common.ConversionModelFactory;
import com.apache.passport.common.SsoConversionModel;
import com.apache.passport.entity.UctUser;
import com.apache.tools.DataMap;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * description:
 *
 * @author dyh 创建时间：2019年03月03日
 */
public abstract class SuperPluginConnector implements PluginConnector {

    protected IDao uctUserDao;

    public void setUctUserDao(IDao uctUserDao) {
        this.uctUserDao = uctUserDao;
    }
    /**
     * description:  根据ename查询用户信息
     * @return
     * @author Hou Dayu
     * @update 2014-12-19
     */
    protected Object getInfoByEname(String userEname) {
        String sql = uctUserDao.getSql(3);
        MethodParam param = new MethodParam("ByUserEname", "", sql, "com.apache.passport.entity.UctUser");
        if(ConversionModelFactory.getInstance().isEnable()){
            SsoConversionModel model = ConversionModelFactory.getInstance().getModel();
            sql = model.sqlByUserEname(userEname,false);
            try {
                Connection conn = ConnctionData.getInstance().getConnection(ConversionModelFactory.USER_DB_KEY);
                Map list =  ConnctionData.getInstance().selectInfo(conn, sql);
                if (Validator.isEmpty(list))
                    return null;
                return getUctUser((DataMap<String>)list);
            }catch (SQLException e){
                return null;
            }
        }else {
            param.setParams("userEname", userEname);
            return uctUserDao.selectSingle(param);
        }
    }

    protected Object getUserByEname(String userEname) {
        String sql = uctUserDao.getSql(3);
        MethodParam param = new MethodParam("ByUserEname", "", sql, "com.apache.passport.entity.UctUser");
        String mobileLogin = SystemTools.getInstance().getValue("mobileLogin");
        String emailLogin = SystemTools.getInstance().getValue("emailLogin");
        if(ConversionModelFactory.getInstance().isEnable()){
            SsoConversionModel model = ConversionModelFactory.getInstance().getModel();
            boolean mark = "1".equals(mobileLogin)?true:false;
            sql = model.sqlByUserEname(userEname,mark);
            try {
                Connection conn = ConnctionData.getInstance().getConnection(ConversionModelFactory.USER_DB_KEY);
                List list =  ConnctionData.getInstance().selectListInfo(conn, sql);
                if (Validator.isEmpty(list))
                    return null;
                return getUctUser((DataMap<String>)list.get(0));
            }catch (SQLException e){
                return null;
            }
        }else {
            param.setParams("userEname", userEname);
            if ("1".equals(mobileLogin)) {
                param.setParams("mobile", userEname);
            }
            if ("1".equals(emailLogin)) {
                param.setParams("email", userEname);
            }
            List list = uctUserDao.select(param);
            if (Validator.isEmpty(list))
                return null;
            return list.get(0);
        }
    }

    /**
     * description:  根据ename,手机号,邮箱查询用户信息
     *
     * @param vo
     * @return
     */
    protected Object getInfoByEname(ParamsVo<UctUser> vo) {
        UctUser user = vo.getObj();
        String userEname = user.getUserEname();
        if (Validator.isNull(userEname)) {
            return null;
        }
        String sql = uctUserDao.getSql(3);// from UctUser where userEname :=userEname
        String mobileLogin = SystemTools.getInstance().getValue("mobileLogin");
        String emailLogin = SystemTools.getInstance().getValue("emailLogin");
        String orgEname = user.getOrgEname();

        MethodParam param = new MethodParam("ByUserEname", "", uctUserDao.getSql(3),
                "com.apache.passport.entity.UctUser");
        if(ConversionModelFactory.getInstance().isEnable()){
            SsoConversionModel model = ConversionModelFactory.getInstance().getModel();
            boolean mark = "1".equals(mobileLogin)?true:false;
            sql = model.sqlByUserEname(userEname,mark);
            try {
                Connection conn = ConnctionData.getInstance().getConnection(ConversionModelFactory.USER_DB_KEY);
                List list =  ConnctionData.getInstance().selectListInfo(conn, sql);
                if (Validator.isEmpty(list))
                    return null;
                return getUctUser((DataMap<String>)list.get(0));
            }catch (SQLException e){
                return null;
            }
        }else {
            param.setParams("userEname", userEname);
            if (Validator.isNotNull(orgEname)) {
                param.setParams("orgEname", orgEname);
            }
            if ("1".equals(mobileLogin)) {
                param.setParams("mobile", userEname);
            }
            if ("1".equals(emailLogin)) {
                param.setParams("email", userEname);
            }
            List list = uctUserDao.select(param);
            if (Validator.isEmpty(list))
                return null;
            return list.get(0);
        }
    }

    /**
     * description:  根据邮箱查询用户信息
     * @param email
     * @return
     */
    public Object getInfoByEmail(String email) {
        String sql = uctUserDao.getSql(4);
        MethodParam param = new MethodParam("ByEmail", "", sql, "com.apache.passport.entity.UctUser");
        if(ConversionModelFactory.getInstance().isEnable()){
            SsoConversionModel model = ConversionModelFactory.getInstance().getModel();
            sql = model.sqlByEmail(email);
            try {
                Connection conn = ConnctionData.getInstance().getConnection(ConversionModelFactory.USER_DB_KEY);
                List list =  ConnctionData.getInstance().selectListInfo(conn, sql);
                if (Validator.isEmpty(list))
                    return null;
                return getUctUser((DataMap)list.get(0));
            }catch (SQLException e){
                return null;
            }
        }else {
            param.setParams("email", email);
            return uctUserDao.selectSingle(param);
        }
    }

    /**
     * description:  根据手机号查询用户信息
     * @param mobile
     * @return
     */
    public Object getInfoByMobile(String mobile) {
        String sql = uctUserDao.getSql(5);
        MethodParam param = new MethodParam("ByMobile", "", sql, "com.apache.passport.entity.UctUser");
        if(ConversionModelFactory.getInstance().isEnable()){
            SsoConversionModel model = ConversionModelFactory.getInstance().getModel();
            sql = model.sqlByMobile(mobile);
            try {
                Connection conn = ConnctionData.getInstance().getConnection(ConversionModelFactory.USER_DB_KEY);
                List list =  ConnctionData.getInstance().selectListInfo(conn, sql);
                if (Validator.isEmpty(list))
                    return null;
                return getUctUser((DataMap<String>)list.get(0));
            }catch (SQLException e){
                return null;
            }
        }else {
            param.setParams("mobile", mobile);
            return uctUserDao.selectSingle(param);
        }
    }

    protected UctUser getUctUser(DataMap<String> map){
        UctUser uctUser = null;
        if(null != map){
            uctUser = new UctUser();
            uctUser.setUserEname(map.get("userEname"));
            uctUser.setUserCname(map.get("userCname"));
            uctUser.setEmail(map.get("email"));
            uctUser.setMobile(map.get("mobile"));
            uctUser.setUserPass(map.get("userPass"));
            uctUser.setUserStatus(Integer.parseInt(map.get("userStatus")));
            uctUser.setRegIp(map.get("regIp"));
            uctUser.setDelStatus(map.get("delStatus"));
            uctUser.setUserType(map.get("userType"));
            uctUser.setCustomerType(map.get("customerType"));
        }
        return uctUser;
    }

}
